/*
 * Copyright 2008 Max Kugland
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.splink.library.queue {
	import org.splink.library.distributor.Distributor;

	/**
	 * Abstract convenience class for classes wishing to implement <code>IQable</code>
	 * with very little effort.
	 * 
	 * @see org.splink.library.queue.IQable
	 * 
	 * @author Max Kugland
	 */
	public class Qable extends Distributor implements IQable {
		private var _id : String;

		public function Qable(id : String) {
			_id = id;
		}

		/**
		 * @inheritDoc
		 */
		final public function start() : void {
			distribute(new QEvent(this, QEvent.START, _id));
			doStart();
		}

		/**
		 * Abstract
		 */
		protected function doStart() : void {
		}

		/**
		 * @inheritDoc
		 */
		final public function stop() : void {
			doStop();
		}

		/**
		 * Abstract
		 */
		protected function doStop() : void {
		}

		/**
		 * @inheritDoc
		 */
		final public function reset() : void {
			doReset();
		}

		/**
		 * Abstract
		 */
		protected function doReset() : void {
		}
		
		final protected function complete() : void {
			distribute(new QEvent(this, QEvent.COMPLETE, _id));
		}

		final protected function error(msg : String) : void {
			var e : QEvent = new QEvent(this, QEvent.ERROR, _id);
			e.errorMessage = msg;
			distribute(e);
		}

		/**
		 * @inheritDoc
		 */
		final public function get id() : String {
			return _id;
		}

		/**
		 * @inheritDoc
		 */
		final override public function finalize() : void {
			super.finalize();
			doFinalize();
		}

		/**
		 * Abstract
		 */
		protected function doFinalize() : void {
		}
	}
}
